package com.ruoyi.common.enums;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;

public class BaseStringDeserializer extends StdDeserializer<BaseString> {

    public BaseStringDeserializer() {
        super(BaseString.class);
    }

    @Override
    public BaseString deserialize(JsonParser p, DeserializationContext ctxt)
            throws IOException {
        // 获取当前处理的字段类型
        JavaType type = ctxt.getContextualType();
        Class<? extends BaseString > enumClass = (Class<? extends BaseString>) type.getRawClass();

        // 读取JSON中的值
        JsonNode node = p.getCodec().readTree(p);
        String code = node.asText();

        // 使用接口中的静态方法进行转换
        return BaseString.fromCode(enumClass, code);
    }
}
